import time
from io import BytesIO

import ddddocr
import pytesseract
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

# 1.创建浏览器驱动对象
driver=webdriver.Edge()
driver.maximize_window()
# 2.通过get方法获取网页
driver.get("http://192.168.1.14:8080/")
time.sleep(0.5)
driver.find_element(By.XPATH,"//a[contains(text(),'解读回应')]").click()
time.sleep(0.5)
driver.find_element(By.CSS_SELECTOR,".write-button").click()
# 切换到新窗口
handles=driver.window_handles
driver.switch_to.window(handles[1])
time.sleep(0.5)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(10)
driver.find_element(By.CSS_SELECTOR,"#woyao").click()
time.sleep(0.5)
driver.find_element(By.CSS_SELECTOR,"input[placeholder='请输入姓名']").send_keys("张三")
# input[placeholder='请输入身份证'] css
driver.find_element(By.CSS_SELECTOR,"input[placeholder='请输入身份证']").send_keys("1234567890")
# 下拉框
select=Select(driver.find_element(By.ID,"sq_vocation"))
select.select_by_index(3)
# input[placeholder='请输入您的电子邮箱'] css
driver.find_element(By.CSS_SELECTOR,"input[placeholder='请输入您的电子邮箱']").send_keys("280735063@qq.com")
# input[placeholder='请输入您通讯地址'] css
driver.find_element(By.CSS_SELECTOR,"input[placeholder='请输入您通讯地址']").send_keys("中国")
# 按钮 css #pur_1
driver.find_element(By.CSS_SELECTOR,"#pur_1").click()
# input[placeholder='请输入信件标题'] css
driver.find_element(By.CSS_SELECTOR,"input[placeholder='请输入信件标题']").send_keys("事件")
# textarea[placeholder='请输入信件内容'] css
driver.find_element(By.CSS_SELECTOR,"textarea[placeholder='请输入信件内容']").send_keys("  xxxxxxxxxxxxxxxxxxx")
# css input[placeholder='请输入您的手机号码'] 17630135209
driver.find_element(By.CSS_SELECTOR,"input[placeholder='请输入您的手机号码']").send_keys("17630135209")
# 验证码
code_img_element = driver.find_element(By.CSS_SELECTOR, ".login-code-img")
img_binary=code_img_element.screenshot_as_png
# print(img_binary)
# 将图片保存到本地
with open("code.png", "wb") as f:
    f.write(img_binary)  # 写入二进制数据生成图片
img = Image.open("code.png")  # 读取本地图片
# 读取截图并识别
ocr = ddddocr.DdddOcr()
image = open("code.png", "rb").read()
result = ocr.classification(image)
# print(result)
# xpath //div[6]//div[2]//div[1]//div[1]//div[1]//input[1]
driver.find_element(By.XPATH,"//div[6]//div[2]//div[1]//div[1]//div[1]//input[1]").send_keys(result)
time.sleep(0.5)
# css #btnOK
driver.find_element(By.CSS_SELECTOR,"#btnOK").click()
time.sleep(2)
# #result_sq_code css text
letter_coding=driver.find_element(By.CSS_SELECTOR,"#result_sq_code").text
# #result_query_code css text 密码
password=driver.find_element(By.CSS_SELECTOR,"#result_query_code").text
# input[placeholder='请输入信件编号'] css letter_coding
driver.find_element(By.CSS_SELECTOR,"input[placeholder='请输入信件编号']").send_keys(letter_coding)
# input[placeholder='请输入查询码'] css password
driver.find_element(By.CSS_SELECTOR,"input[placeholder='请输入查询码']").send_keys(password)
# div[class='search'] p button css 点击
driver.find_element(By.CSS_SELECTOR,"div[class='search'] p button").click()
# 弹框 div[class='el-message-box__message'] p css 获取弹框内容
time.sleep(1)
message=driver.find_element(By.CSS_SELECTOR,"div[class='el-message-box__message'] p").text
print("弹框内容:",message)
time.sleep(2)
# 4.关闭浏览器对象
driver.quit()  # 关闭浏览器